package com.demo.rydemo.config;

import com.demo.rydemo.shiro.filter.CustomAccessControlFilter;
import com.demo.rydemo.shiro.realm.MyShiroRealm;
import com.demo.rydemo.shiro.session.CustomSessionManager;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author ry
 * @Description  https://www.jianshu.com/p/f485c756096c
 * @Date 2020/8/3 15:45
 */
@ConditionalOnProperty(name = "com.demo.loginMode", havingValue = "shiro")
//@Configuration
public class ShiroConfig {

    @Autowired
    CustomSessionManager customSessionManager;

    @Autowired
    CustomAccessControlFilter customAccessControlFilter;

    @Bean
    public SecurityManager securityManager(){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(myShiroRealm());
        //设置 session管理器
        securityManager.setSessionManager(customSessionManager);
        return securityManager;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(securityManager);

        Map<String,String> filterChainDefinitionMap = new LinkedHashMap<>();

        HashMap filterMap = new HashMap();
//        filterMap.put("customAccessControlFilter",customAccessControlFilter);
        bean.setFilters(filterMap);

        filterChainDefinitionMap.put("/static/**","anon");
        filterChainDefinitionMap.put("/d_user/d-user/login", "anon");
        filterChainDefinitionMap.put("/logout","logout");
        //登录地址 *配置无须认证
        filterChainDefinitionMap.put("/**", "authc");

//        filterChainDefinitionMap.put("/**", "customAccessControlFilter");//自定义拦截器

        //配置登录地址、登录成功地址、配置未授权地址
        bean.setLoginUrl("/loginSuccess");
//        bean.setSuccessUrl("http://localhost:8080/");
        bean.setUnauthorizedUrl("/unauthorized");

        bean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return bean;
    }

    @Bean
    public MyShiroRealm myShiroRealm(){
        MyShiroRealm realm = new MyShiroRealm();
        realm.setCredentialsMatcher(hashedCredentialsMatcher());
        return realm;
    }



    //加密设置
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher(){
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //加密方式
        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        //加密次数
        hashedCredentialsMatcher.setHashIterations(1);
        //存储散列后的密码是否为16进制
        //hashedCredentialsMatcher.isStoredCredentialsHexEncoded();
        return hashedCredentialsMatcher;
    }
}
